$(function() {
	var service = new EbeimService();
	var json = {};
	json.level = '';
	json.type = '';
	json.begin = '';
	json.end = '';
	var user = plugin.cookieToJson(document.cookie);
    var jusername = $("#j-username"),
    	jrole = $("#j-role");
    
    jusername.html(user.username);
    if(user.role == "SYSADMIN"){
    	jrole.html("系统管理员");
    }else if(user.role == "ADMIN"){
    	jrole.html("管理员");
    }else{
    	jrole.html("普通用户");
    }
	service.getMonitorAlert(json, callback);
})
var callback = function(data) {
	var item = data.list;
	var str = '';
	if (typeof (item) != "undefined") {
		str += '<table>';
		for (i = 0; i < item.length; i++) {
			str += '<tr class="error">';
			str += '<td width="100px">' + getType(item[i].warn_type) + '</td>';
			str += '<td width="70px">' + getlevel(item[i].warn_level) + '</td>';
			str += '<td width="130px">' + getTime(item[i].warn_time) + '</td>';
			str += '<td width="70px">' + getPart(item[i].warn_part) + '</td>';
			str += '<td width="160px">' + item[i].warn_desc + '</td>';
			str += '<td width="100px"><a href="javascript:;">查看明细</a></td>';
			str += '</tr>';
		}
		str += '</table>';
	}
	$("div.table-body").html(str);

}
function getType(n) {
	var str = '';
	switch (n) {
	case 0:
		str = '监控预警';
		break;
	case 1:
		str = '软件更新';
		break;
	case 2:
		str = '任务执行';
		break;
	default:
		str = '';
	}
	return str;
}
function getlevel(n) {
	var str = '';
	switch (n) {
	case 0:
		str = '严重';
		break;
	case 1:
		str = '一般';
		break;
	case 2:
		str = '提示';
		break;
	default:
		str = '';
	}
	return str;
}
function getPart(n) {
	var str = '';
	switch (n) {
	case 0:
		str = '系统';
		break;
	case 1:
		str = '引擎';
		break;
	default:
		str = '';
	}
	return str;
}

function serchWarn() {
	var service = new EbeimService();
	var json = getSerchDate();
	/*
	 * json.level = $("#level").val(); json.type = $("#type").val(); json.time =
	 * strToDate($("#time").val());
	 */
	service.getMonitorAlert(json, callback);
}
function getSerchDate() {
	var json = {};
	json.level = $("#level").val();
	json.type = $("#type").val();
	var time = $("#time").val();
	if (time.length == 0) {
		json.begin = '';
		json.end = '';
	} else {

		var dateStrs = time.split("-");
		var year = parseInt(dateStrs[0], 10);
		var month = parseInt(dateStrs[1], 10) - 1;
		var day = parseInt(dateStrs[2], 10);
		var begin = new Date(year, month, day, 0, 0, 0);
		var end = new Date(year, month, day, 23, 59, 59);
		json.begin = begin.getTime();
		json.end = end.getTime();

	}
	return json;
}

/*
 * function strToDate(str,json) { if (str.length != 0) { var tempStrs =
 * str.split(" "); var dateStrs = tempStrs[0].split("-"); var year =
 * parseInt(dateStrs[0], 10); var month = parseInt(dateStrs[1], 10); var day =
 * parseInt(dateStrs[2], 10); var timeStrs = tempStrs[1].split(":"); var hour =
 * parseInt(timeStrs[0], 10); var minute = parseInt(timeStrs[1], 10); var second =
 * parseInt(timeStrs[2], 10); var date = new Date(year, month, day, hour,
 * minute, second); return date.getTime(); } else { return ''; } }
 */
//function getTime(nS) {
//	var time = new Date(parseInt(nS)).toLocaleString().replace(/年|月/g, "-")
//			.replace(/日/g, " ");
//	var mm = time.split(" ");
//	return mm[0];
//}
function getTime(time) {
	var date = new Date(time);
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	var hour = date.getHours();
	var minute = date.getMinutes();
	var second = date.getSeconds();

	return year + "-" + checkTime(month) + "-" + checkTime(day) ;
}
function checkTime(time) {
	if (time < 10) {
		time = "0" + time;
	}
	return time;
}